package com.kaili.basic.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.vo.MdmDeviceSignDetailVO;
import com.kaili.basic.pojo.vo.MdmDeviceSignLocationVo;
import com.kaili.basic.pojo.vo.MdmDeviceSignVO;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.aop.OperateLogAspect;
import com.kaili.common.aop.log.enums.FunctionName;
import com.kaili.common.aop.log.enums.OperateType;
import com.kaili.common.response.ResponseData;
import com.kaili.common.response.RestStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.exception.RestException;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

import com.kaili.basic.service.IDeviceSignService;


/**
*  控制器
* @author:zhouyijian
* @Time: 2019-07-24
* @Copyright: ©  杭州凯立通信有限公司 版权所有
* @Warning: 本内容仅限于公司内部传阅,禁止外泄或用于其它商业目的
*/
@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Api(tags = "设备签到", description = "设备签到相关API")
public class MdmDeviceSignController {
	@Autowired
	IDeviceSignService deviceSignService;

	@GetMapping(value = "/deviceSign/total")
	@ApiOperation("设备签到汇总查询")
	public List<MdmDeviceSignVO> querySignTotal(@ModelAttribute MdmDeviceSignQueryDto dto) {
		return deviceSignService.querySignTotal(dto);
	}

	@GetMapping(value = "/deviceSign/detail")
	@ApiOperation("设备已签/未签详情分页查询")
	public Page<MdmDeviceSignDetailVO> queryPageList(@ModelAttribute MdmDeviceSignQueryDto dto) {
		return deviceSignService.queryPageListDetail(dto);
	}

	@PostMapping(value = "/deviceSign")
	@ApiOperation("设备签到")
	@OperateLogAspect(functionName = FunctionName.deviceSign,operateType = OperateType.ADD, key = "imei")
	public ResponseData addSignRecord(@RequestBody @Validated MdmDeviceOnlineAddDto dto) {
		return deviceSignService.doCreateDeviceSign(dto);
	}

	@PostMapping(value = "/pdaDeviceSign")
	@ApiOperation("PDA设备签到")
	@OperateLogAspect(functionName = FunctionName.deviceSign,operateType = OperateType.ADD, key = "imei")
	public ResponseData addPdaSignRecord(@RequestBody @Validated MdmDeviceOnlineAddDto dto) {
		return deviceSignService.doCreateDeviceSign(dto);
	}

    @PutMapping(value = "/deviceSign/pdaUpdate")
    @ApiOperation("PDA设备签到更新")
    @OperateLogAspect(functionName = FunctionName.deviceSign,operateType = OperateType.UPDATE, key = "imei")
    public RestStatus update(@RequestBody @Validated MdmDeviceOnlineAddDto dto) {
        return deviceSignService.updateDeviceSignByImei(dto);
    }

	@GetMapping(value = "/deviceSign/export")
	@ApiOperation("设备签到导出")
	public void export(HttpServletResponse response, @ModelAttribute MdmDeviceSignQueryDto dto) throws RestException {
		List<MdmDeviceSignDetailVO>  result = deviceSignService.queryListByCondition(dto);
		ExcelUtil.exportExcel(result,"设备签到详情导出","", MdmDeviceSignVO.class, UUID.randomUUID().toString()+".xls",response);
	}

	/**
	* @Desc:
	* @author: shenliqiang
	* @Time: 2019/7/29 19:49
	* @param dto
	* @return java.lang.String[][]
	*/

	@GetMapping(value = "/deviceSign/deviceLocations")
	@ApiOperation(value = "获取设备最新位置",notes = "返回设备最新位置,返回数据为null代表当天位置更新情况",httpMethod = "GET")
	public List<MdmDeviceSignLocationVo> queryDeviceSignLocation(@ModelAttribute MdmDeviceSignLocationDto dto){
		return this.deviceSignService.queryDeviceLocation(dto);
	}

	/**
	* @Desc:
	* @author: shenliqiang
	* @Time: 2019/7/29 19:49
	* @param dto
	* @return java.lang.String[][]
	*/

	@GetMapping(value = "/deviceSign/deviceTrack")
	@ApiOperation(value = "获取设备轨迹",notes = "数组为降序，即第一组为最新经纬度；返回数组为空则表明当天无轨迹",httpMethod = "GET")
	public String[][] queryDeviceTrack(@ModelAttribute MdmDeviceSignLocationDto dto){
		return this.deviceSignService.queryDeviceTrack(dto);
	}
}
